#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-

=begin 
@author = sirius
@time = 2017.11.13
=end

require 'mysql2'

client = Mysql2::Client.new(
    :host     => '127.0.0.1', # 主机
    :username => 'root',      # 用户名
    :password => 'root',      # 密码
    :database => 'StudentDB', # 数据库
    :encoding => 'utf8'       # 编码
    )

results = client.query("SELECT * FROM Student")
puts results.count

# 迭代结果集
results.each do |row|
  # row 是哈希
  # 键值是数据库字段
  # 值都是对应 MySQL中数据
  puts row["Sno"] # row["id"].class == Fixnum
  if row["Sname"]  # 不存在则是 nil
    puts row["Sname"]
  end
end